"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomResultDao = void 0;
const Config_1 = require("../../src/config/Config");
const DbConfig_1 = require("../../src/config/DbConfig");
const RoomConfig_1 = require("../../src/config/RoomConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
class RoomResultDao extends DaoBase_1.DaoBase {
    static insert_room_result(model) {
        return __awaiter(this, void 0, void 0, function* () {
            if (model.gameConf["roomType"] == RoomConfig_1.RoomConfig.Room_Type.Create) {
                let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
                let sql = "insert into t_room_result (gameType, gameConf, playerList, room_id, clubid, time, bigWin, userid1,userid2,userid3,userid4,userid5) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ;";
                let sqlParams = [model.gameType, JSON.stringify(model.gameConf), JSON.stringify(model.playerList), model.room_id, model.clubid,
                    DateUtil_1.default.formatDateStr(model.time), model.bigWin, model.userid1, model.userid2, model.userid3, model.userid4, model.userid5];
                let rows = yield client.queryPromise(sql, sqlParams);
                Logger_1.Logger.log("insert_room_result====", rows);
                if (rows && rows["affectedRows"] > 0) {
                    return rows["insertId"];
                }
                else {
                    return 0;
                }
            }
            return 0;
        });
    }
    static update_room_result(model) {
        return __awaiter(this, void 0, void 0, function* () {
            if (model.gameConf["roomType"] == RoomConfig_1.RoomConfig.Room_Type.Create) {
                let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
                let sql = "update t_room_result set `playerList`=?,`time`=?, `bigWin`=? where `id`=? ;";
                let sqlParams = [JSON.stringify(model.playerList),
                    DateUtil_1.default.formatDateStr(model.time), model.bigWin, model.id];
                let rows = yield client.queryPromise(sql, sqlParams);
                Logger_1.Logger.log("update_room_result====", rows);
                if (rows && rows["affectedRows"] > 0) {
                    return true;
                }
                else {
                    return false;
                }
            }
        });
    }
    static checkClearRoomResult(delete_time) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "delete from t_room_result where time < ?";
            let sqlParams = [DateUtil_1.default.formatDateStr(delete_time)];
            yield client.queryPromise(sql, sqlParams);
        });
    }
    static getResultByGameTypeAndClubIdAndUserid(gameType, clubid, userid, page, pageSize, searchTime) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "";
            let sqlParams = [];
            let startTime = searchTime;
            let endTime = startTime + Config_1.Config.OneDay;
            let startTimeStr = DateUtil_1.default.formatDateStr(startTime);
            let endTimeStr = DateUtil_1.default.formatDateStr(endTime);
            // let start: number = (page - 1) * pageSize
            if (userid > 0) {
                sql = "select * from  t_room_result where `gameType`=? and `clubid`=? and (`userid1`=? or `userid2`=? or `userid3` =? or `userid4`=? or `userid5`=?) and `time` >= ? and `time`< ? ";
                sqlParams = [gameType, clubid, userid, userid, userid, userid, userid, startTimeStr, endTimeStr];
            }
            else {
                sql = "select * from  t_room_result where `gameType`=? and `clubid`=? and `time` >= ? and `time`< ? ";
                sqlParams = [gameType, clubid, startTimeStr, endTimeStr];
            }
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getResultByGameTypeAndClubIdAndUserid", sql, sqlParams, rows);
            if (rows && rows.length > 0) {
                return rows;
            }
            return [];
        });
    }
    static getResultByGameTypeAndUserid(gameType, userid, page, pageSize, searchTime) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select * from  t_room_result  where ";
            let sqlParams = [];
            let startTime = searchTime;
            let endTime = startTime + Config_1.Config.OneDay;
            let startTimeStr = DateUtil_1.default.formatDateStr(startTime);
            let endTimeStr = DateUtil_1.default.formatDateStr(endTime);
            // let start: number = (page - 1) * pageSize
            if (gameType > 0) {
                sql += "  `gameType`=" + gameType + " and ";
            }
            if (userid > 0) {
                sql += "   (`userid1`=? or `userid2`=? or `userid3` =? or `userid4`=? or `userid5`=?) and `time` >= ? and `time`< ? ";
                sqlParams = [userid, userid, userid, userid, userid, startTimeStr, endTimeStr];
            }
            else {
                sql += " `time` >= ? and `time`< ? ";
                sqlParams = [startTimeStr, endTimeStr];
            }
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getResultByGameTypeAndUserid", sql, sqlParams, rows);
            if (rows && rows.length > 0) {
                return rows;
            }
            return [];
        });
    }
}
exports.RoomResultDao = RoomResultDao;
RoomResultDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_admin;
RoomResultDao.Tb_Name = "t_room_result";
